import React from 'react'

const useWebsocket = (url:string) => {
    const ws = new WebSocket(url)
    const sendMsg = (msg:string) => {
        ws.send(msg)
    }
    const [data, setData] = React.useState('')
    ws.addEventListener('message', function message(e) {
        setData(e.data)
    })
  return {
    sendMsg,
    data
  }
}

export default useWebsocket
